package joe;

import org.jetbrains.annotations.NotNull;
import static joe.UnsafeUtil.getFloat;
import static joe.UnsafeUtil.putFloat;

public class Vec4 {
	public final long ptr = Engine.current().getMemPool().alloc16(this);

	public @NotNull Vec4 zero() {
		putFloat(ptr, 0, 0, 0, 0);
		return this;
	}

	public @NotNull Vec4 one() {
		putFloat(ptr, 1, 1, 1, 1);
		return this;
	}

	public @NotNull Vec4 identity() {
		putFloat(ptr, 0, 0, 0, 1);
		return this;
	}

	/**
	 * @param i 0..3
	 */
	public float get(int i) {
		return getFloat(ptr + i * 4L);
	}

	/**
	 * @param i 0..3
	 */
	public @NotNull Vec4 set(int i, float v) {
		putFloat(ptr + i * 4L, v);
		return this;
	}

	public @NotNull Vec4 set(float x, float y, float z) {
		putFloat(ptr, x, y, z, 1);
		return this;
	}

	public @NotNull Vec4 set(float x, float y, float z, float w) {
		putFloat(ptr, x, y, z, w);
		return this;
	}

	public @NotNull Vec4 set(@NotNull Vec4 v) {
		UnsafeUtil.copyMemory(v.ptr, ptr, 16);
		return this;
	}

	public @NotNull Vec4 add(float x, float y, float z) {
		final var p = ptr;
		putFloat(p, getFloat(p) + x, getFloat(p + 4) + y, getFloat(p + 8) + z);
		return this;
	}

	public @NotNull Vec4 mul(@NotNull Mat4 m) {
		final var p = ptr;
		UnsafeUtil.matVecMulAvx(p, m.ptr, p);
		return this;
	}

	@Override
	public String toString() {
		return "(" + get(0) + "," + get(1) + "," + get(2) + "," + get(3) + ")";
	}
}
